package com.approval.approvalapp.index.dbsx.ptbl.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.PtblXqBean;
import com.approval.approvalapp.index.dbsx.ptbl.fragmetn.PtBlYbFragment;
import com.approval.approvalapp.index.dbsx.ptbl.fragmetn.PtblDbFragment;
import com.approval.approvalapp.index.dbsx.tshf.BaseActivity2;
import com.approval.approvalapp.util.DensityUtil;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

//普通办理处理
public class PtblClActivity extends BaseActivity2 {

    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.et)
    EditText et;
    @Bind(R.id.xyblc_tv)
    TextView xyblcTv;
    @Bind(R.id.rv)
    RecyclerView rv;
    @Bind(R.id.gq_tv)
    TextView gqTv;
    @Bind(R.id.bybl_tv)
    TextView byblTv;
    @Bind(R.id.tj_tv)
    TextView tjTv;
    @Bind(R.id.xybshr_tv)
    TextView xybshr_tv;
    @Bind(R.id.xyblctitle_tv)
    TextView xyblctitle_tv;
    private List<PtblXqBean.NextStepInfoBean.UsersInfoBean> usersInfoList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_ptbl_cl;
    }

    @Override
    public void initView() {
        String str = getIntent().getStringExtra("str");
        xyblcTv.setText(str);
        usersInfoList = (List<PtblXqBean.NextStepInfoBean.UsersInfoBean>) getIntent().getSerializableExtra("list");
    }

    @Override
    public void initData() {
        rv.setLayoutManager(new GridLayoutManager(this, 3));
        PtblClAdapter ptblClAdapter = new PtblClAdapter();
        ptblClAdapter.setList(usersInfoList);
        rv.setAdapter(ptblClAdapter);

        if (usersInfoList == null || usersInfoList.size() == 0) {
            xybshr_tv.setVisibility(View.GONE);
            xyblctitle_tv.setVisibility(View.GONE);
        }
    }


    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }


    @OnClick({R.id.gq_tv, R.id.bybl_tv, R.id.tj_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.gq_tv:
                new XPopup.Builder(this)
                        .autoOpenSoftInput(true)
                        .asCustom(new CustomPopup(this))
                        .show();
                break;
            case R.id.bybl_tv:
                if (TextUtils.isEmpty(et.getText().toString())) {
                    Toast.makeText(this, "请填写处理意见", Toast.LENGTH_SHORT).show();
                    return;
                }
                JSONObject jsonObject = new JSONObject();

                jsonObject.put("cid", PtblXqDbActivity.instance.cid);
                jsonObject.put("bsnum", PtblXqDbActivity.instance.bsnum);
                if (PtblXqDbActivity.instance.is_sign) {
                    jsonObject.put("is_sign", "1");
                } else {
                    jsonObject.put("is_sign", "0");
                }
                jsonObject.put("opinion", et.getText().toString());


                Log.d("PtblClActivitysss", jsonObject.toString());
                NetRequsetUtil.getInstance().netRequestPostJson(
                        "v1/user/not_allowed",
                        jsonObject.toString(),
                        new NetDownResponse() {
                            @Override
                            public void success(String str) {
                                Toast.makeText(PtblClActivity.this, "处理成功", Toast.LENGTH_SHORT).show();
                                if (PtblXqDbActivity.instance != null) {
                                    PtblXqDbActivity.instance.finish();
                                }
                                if (PtblDbFragment.instance != null){
                                    PtblDbFragment.instance.refreshData();
                                }
                                if (PtBlYbFragment.instance != null){
                                    PtBlYbFragment.instance.refreshData();
                                }
                                PtblClActivity.this.finish();
                            }

                            @Override
                            public void errowithresponse(String str) {
                                Log.d("PtblClActivitysss", str);
                                ErrorBeanUtil.getErrorMessage(str);
                            }

                            @Override
                            public void erro() {

                            }

                            @Override
                            public void finish() {

                            }
                        });
                break;
            case R.id.tj_tv:
                JSONObject jsonObject1 = new JSONObject();

                jsonObject1.put("name", SpUtil.getString(this,
                        SpUtil.USER_RUN_NICKNAME, ""));
                jsonObject1.put("cid", PtblXqDbActivity.instance.cid);
                jsonObject1.put("bsnum", PtblXqDbActivity.instance.bsnum);
                jsonObject1.put("data_id", PtblXqDbActivity.instance.data_id);
                jsonObject1.put("from_id", PtblXqDbActivity.instance.from_id);
                if (TextUtils.isEmpty(et.getText().toString())) {
                    Toast.makeText(this, "建议内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                jsonObject1.put("option", et.getText().toString());
                if (PtblXqDbActivity.instance.is_sign) {
                    jsonObject1.put("is_sign", "1");
                } else {
                    jsonObject1.put("is_sign", "0");
                }

                if (usersInfoList != null && usersInfoList.size() > 0) {
                    List<PtblXqBean.NextStepInfoBean.UsersInfoBean> list = new ArrayList<>();
                    for (int i = 0; i < usersInfoList.size(); i++) {
                        if (usersInfoList.get(i).isChoose()) {
                            list.add(usersInfoList.get(i));
                        }

                    }
                    if (list.size() == 0) {
                        Toast.makeText(this, "请选择下一步处理人", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    jsonObject1.put("userinfo" , list);
                    jsonObject1.put("nodeId", PtblXqDbActivity.instance.nodeId);
                    jsonObject1.put("nodeName", PtblXqDbActivity.instance.nodeName);
                    jsonObject1.put("condition", PtblXqDbActivity.instance.condition);
                    jsonObject1.put("gateway", PtblXqDbActivity.instance.gateway);
                }


                Log.d("2323", jsonObject1.toString());
                NetRequsetUtil.getInstance().netRequestPostJson("v1/user/submit_busi",
                        jsonObject1.toString(),
                        new NetDownResponse() {
                            @Override
                            public void success(String str) {
                                Toast.makeText(PtblClActivity.this, "处理成功", Toast.LENGTH_SHORT).show();
                                if (PtblXqDbActivity.instance != null) {
                                    PtblXqDbActivity.instance.finish();
                                }
                                if (PtblDbFragment.instance != null){
                                    PtblDbFragment.instance.refreshData();
                                }
                                if (PtBlYbFragment.instance != null){
                                    PtBlYbFragment.instance.refreshData();
                                }
                                PtblClActivity.this.finish();
                            }

                            @Override
                            public void errowithresponse(String str) {
                                ErrorBeanUtil.getErrorMessage(str);
                            }

                            @Override
                            public void erro() {

                            }

                            @Override
                            public void finish() {

                            }
                        });
                break;
        }
    }


    class PtblClAdapter extends RecyclerView.Adapter<PtblClAdapter.Holder> {
        private List<PtblXqBean.NextStepInfoBean.UsersInfoBean> usersInfoList;


        public void setList(List<PtblXqBean.NextStepInfoBean.UsersInfoBean> usersInfoList) {
            this.usersInfoList = usersInfoList;

            notifyDataSetChanged();
        }


        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(PtblClActivity.this).inflate(R.layout.item_ptblcl, parent, false);
            Holder holder = new Holder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.tv.setText(usersInfoList.get(position).getUname());
            if (usersInfoList.get(position).isChoose()) {
                holder.iv_case.setBackgroundResource(R.drawable.case_on);
                holder.tv.setTextColor(Color.parseColor("#1465FD"));
            } else {
                holder.iv_case.setBackgroundResource(R.drawable.case_off);
                holder.tv.setTextColor(Color.parseColor("#7D8198"));
            }
            holder.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (usersInfoList.get(position).isChoose()) {
                        usersInfoList.get(position).setChoose(false);
                        notifyDataSetChanged();
                    } else {
                        usersInfoList.get(position).setChoose(true);
                        notifyDataSetChanged();
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            if (usersInfoList == null) {
                return 0;
            } else {
                return usersInfoList.size();
            }
        }

        class Holder extends RecyclerView.ViewHolder {
            ImageView iv_case;
            TextView tv;
            LinearLayout item;

            public Holder(View itemView) {
                super(itemView);
                iv_case = itemView.findViewById(R.id.iv_case);
                tv = itemView.findViewById(R.id.tv);
                item = itemView.findViewById(R.id.item);
            }
        }
    }

    public class CustomPopup extends CenterPopupView {
        Context context;
        private ImageView iv_close;
        private EditText et_gqsx;
        private EditText et_yjsx;
        private TextView qd_tv;

        public CustomPopup(@NonNull Context context) {
            super(context);
            this.context = context;
        }

        @Override
        protected int getImplLayoutId() {
            return R.layout.custom_popup_ptbl;
        }

        @Override
        protected void onCreate() {
            super.onCreate();
            iv_close = findViewById(R.id.iv_close);
            et_gqsx = findViewById(R.id.et_gqsx);
            et_yjsx = findViewById(R.id.et_yjsx);
            qd_tv = findViewById(R.id.qd_tv);

            iv_close.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });

            qd_tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (TextUtils.isEmpty(et_gqsx.getText().toString()) ||
                            TextUtils.isEmpty(et_yjsx.getText().toString())) {
                        Toast.makeText(context, "请输入时间", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    dismiss();

                    JSONObject jsonObject = new JSONObject();

                    jsonObject.put("cid", PtblXqDbActivity.instance.cid);
                    jsonObject.put("bsnum", PtblXqDbActivity.instance.bsnum);
                    jsonObject.put("name", SpUtil.getString(context, SpUtil.USER_RUN_NICKNAME
                            , ""));
                    jsonObject.put("warning", et_yjsx.getText().toString());
                    jsonObject.put("suspendtime", et_gqsx.getText().toString());
                    if (TextUtils.isEmpty(et.getText().toString())) {
                        Toast.makeText(context, "理由不能为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    jsonObject.put("reason", et.getText().toString());
                    if (PtblXqDbActivity.instance.is_sign) {
                        jsonObject.put("is_sign", "1");
                    } else {
                        jsonObject.put("is_sign", "0");
                    }


                    Log.d("CustomPopupss", jsonObject.toString());

                    NetRequsetUtil.getInstance().netRequestPostJson("v1/user/hang_up",
                            jsonObject.toString(),
                            new NetDownResponse() {
                                @Override
                                public void success(String str) {
                                    Toast.makeText(context, "处理成功", Toast.LENGTH_SHORT).show();
                                    if (PtblXqDbActivity.instance != null) {
                                        PtblXqDbActivity.instance.finish();
                                    }
                                    if (PtblDbFragment.instance != null){
                                        PtblDbFragment.instance.refreshData();
                                    }
                                    if (PtBlYbFragment.instance != null){
                                        PtBlYbFragment.instance.refreshData();
                                    }
                                    PtblClActivity.this.finish();
                                }

                                @Override
                                public void errowithresponse(String str) {
                                    ErrorBeanUtil.getErrorMessage(str);
                                }

                                @Override
                                public void erro() {

                                }

                                @Override
                                public void finish() {

                                }
                            });
                }
            });
        }

        @Override
        protected void onShow() {
            super.onShow();
        }

        @Override
        protected int getMaxWidth() {
            return DensityUtil.dip2px(context, 280f);
        }

    }

}
